﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;

namespace Elementum
{
    class Stage8 : Stage
    {
        public Stage8(Game game) : base(game) { }

        float GROUND_ALPHA = 0f;

        float timeTillPuff = 0.0f;
        SmokePlumeParticleSystemForStage6 smokePlume;
        const float TimeBetweenSmokePlumePuffs = 0.5f;

        MechanismGound mechanismGound;
        int focusTime = 120;
        public override void InitializeScenes()
        {
            base.InitializeScenes();
            for (int i = 1; i < 4; i++)
            {
                Animation a = new Animation("images/stages/stage3/city_night" + i);
                a.LayerDepth = 0.9f;
                _backGround.Add(a);
            }
            _backGroundPos = new Vector2(-450, -400);
            _scenceBound = new Rectangle(-300, -300,
               2500,
              650);
            camera.Bound = _scenceBound;
            camera.CameraTransfer = 100;
            camera.MoveSpeed = 3f;
           // basePos = player.Position;

            for (int i = 0; i < 4; i++)
            {
                Animation wallBack = new Animation("images/stages/stage8/stage8_" + i);
                //wallBack.LayerDepth = 0.9f;
                _wallBackGround.Add(wallBack);
            }
            _wallBackGroungPos = new Vector2(-620, -572);

            for (int i = 4; i < 8; i++)
            {
                Animation wallBack = new Animation("images/stages/stage8/stage8_" + i);
                //wallBack.LayerDepth = 0.9f;
                _wallBackGroundOther.Add(wallBack);
            }
            _wallBackGroungOtherPos = new Vector2(-620, 452);

            smokePlume = new SmokePlumeParticleSystemForStage6(this, 10);

            Animation title = new Animation("images/title/stageTitle", new Rectangle(2, 52, 210, 46), new Point(210, 46));
            title.Scale = 1.2f;

            Transition tra;
            tra = new DefauldTransition( 1, 0, 10, 60);
            tra.Register(this);
            tra = new TitleTransition(title, new Vector2(410, 150), 0, 1, 80, 220);
            tra.Register(this);

            GroundObject grount =
            StateFactory.CreateGround(this, 2145, 135, 71, 587, 0f); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 165, 385, 400, 50, 0); grount.Alpha = GROUND_ALPHA;
            StateFactory.CreatekillBund(this, 453, 448, 160, 64, 0); grount =
            StateFactory.CreateGround(this, 9, 313, 88, 25, 0); grount.Alpha = GROUND_ALPHA;

            StateFactory.CreateElementPiece("Stage8Ele_1",this, 970, 176);
            grount =
            StateFactory.CreateGround(this, 733, 385, 400, 50, 0); grount.Alpha = GROUND_ALPHA;
            grount =
            StateFactory.CreateGround(this, -224, 284, 378, 50, 0); grount.Alpha = GROUND_ALPHA;
            StateFactory.CreatePortal("Stage8Portal_1", this, -238, 209, "stage7", "Stage7_Portal_2");
            SensorSwitch sensorSwitch = 
            StateFactory.CreateSensorSwitch(this, 230, 345);
            
            mechanismGound = 
            StateFactory.CreateMechanismGound(this, 449, 261, 164, 10, 0, 0, 52, 0, 1);
            mechanismGound.CurrentAnimation = new Animation("images/items/mechineWall1", new Point(164, 10));
            
            ModifierContain first = new ModifierContain(mechanismGound);
            ModifierContain next = new ModifierContain(mechanismGound);
            //設定機關行為
            PositionModifier m2 = new PositionModifier(mechanismGound.Position, mechanismGound.Position + new Vector2(0, 52), 0, 1);
            PositionModifier m3 = new PositionModifier(mechanismGound.Position + new Vector2(0, 52), mechanismGound.Position, 0, 1);
            first.AddModifier(m2);
            next.AddModifier(m3);
            mechanismGound.PositiveAction.Add(first);
            mechanismGound.NegetiveAction.Add(next); sensorSwitch.AddTarget(mechanismGound);
            MechanismGound mechanismGound1 =
            StateFactory.CreateMechanismGound(this, 1631, 405, 100, 30, 0, 0, -80, 10, 55); //sensorSwitch.AddTarget(mechanismGound1);
            mechanismGound1.CurrentAnimation = new Animation("images/items/mechineWall1", new Point(100, 30));
            sensorSwitch = 
            StateFactory.CreateSensorSwitch(this, 667, 345);sensorSwitch.AddTarget(mechanismGound);
            sensorSwitch = 
            StateFactory.CreateSensorSwitch(this, 1225, 389);sensorSwitch.AddTarget(mechanismGound1);
            sensorSwitch =
            StateFactory.CreateSensorSwitch(this, 1171, 389);
            mechanismGound1 =
            StateFactory.CreateMechanismGound(this, 1505, 224, 100, 30, 0, 0, 80, 0, 45); sensorSwitch.AddTarget(mechanismGound1);
            mechanismGound1.CurrentAnimation = new Animation("images/items/mechineWall1", new Point(100, 30));
            StateFactory.CreateDoor("Stage8Door_S1", this, 890, 310, "Stage8Door_1");
            grount =
            StateFactory.CreateGround(this, 1134, 428, 400, 50, 0); grount.Alpha = GROUND_ALPHA;
            StateFactory.CreateKey("Stage8Key_S2", this, 972, 386, "Stage8Door_2");
            StateFactory.CreateKey("Stage8Key_S1", this, -14, 343, "Stage8Door_1");
            grount =
            StateFactory.CreateGround(this, 1083, 216, 300, 50, 0); grount.Alpha = GROUND_ALPHA;
            grount =
            StateFactory.CreateGround(this, 1258, 141, 50, 200, 0); grount.Alpha = GROUND_ALPHA;
            Floor floor = 
            StateFactory.CreateFloor(this, 1306, 192, 40, 336, 0, 300, 0, 0, 60);//192機器就卡了
            grount =
            StateFactory.CreateGround(this, 1483, 66, 400, 50, 0); grount.Alpha = GROUND_ALPHA;
            StateFactory.CreatekillBund(this, 1670, 484, 672, 64, 0);
            grount =
            StateFactory.CreatekillBund(this, 2292, 550, 571, 64, 0); grount.Alpha = GROUND_ALPHA;
            StateFactory.CreatePortal("Stage8Portal_2", this, 2139, -259, "stage9", "Stage9_Portal_1");
            StateFactory.CreateDoor("Stage8Door_S2", this, 1390, -11, "Stage8Door_2");
            Switch gameSwitch =
            StateFactory.CreateSwitch(this, 1467, 15); gameSwitch.AddTarget(floor);
            grount =
            StateFactory.CreateGround(this, 1405, 226, 100, 30, 0f); grount.Alpha = GROUND_ALPHA;
            StateFactory.CreateElementPiece("Stage8Ele_2", this, 1380, 193);
            grount =
            StateFactory.CreateGround(this, 2513, -374, 126, 431, 0f); grount.Alpha = GROUND_ALPHA;
            grount =
            StateFactory.CreateGround(this, -498, 140, 171, 339, 0f); grount.Alpha = GROUND_ALPHA;
            grount =
            StateFactory.CreateGround(this, 2247, -184, 407, 50, 0f); grount.Alpha = GROUND_ALPHA;
            Mechine mechine = 
            StateFactory.CreateMechine("Stage8_Mechine_1", this, 1167, 151, 2);//mechine.AddTarget("");
            mechanismGound =
            StateFactory.CreateMechanismGound(this, 1728, 66, 90, 50, 0, 0, -50, 0, 30); mechine.AddTarget(mechanismGound);
            mechanismGound.CurrentAnimation = new Animation("images/items/mechineWall1", new Point(90, 50));
            mechanismGound =
            StateFactory.CreateMechanismGound(this, 1818, 66, 90, 50, 0, 0, -100, 0, 60); mechine.AddTarget(mechanismGound);
            mechanismGound.CurrentAnimation = new Animation("images/items/mechineWall1", new Point(90, 50));
            mechanismGound =
            StateFactory.CreateMechanismGound(this, 1908, 66, 90, 50, 0, 0, -150, 0, 90); mechine.AddTarget(mechanismGound);
            mechanismGound.CurrentAnimation = new Animation("images/items/mechineWall1", new Point(90, 50));
            mechanismGound =
            StateFactory.CreateMechanismGound(this, 1998, 66, 90, 50, 0, 0, -200, 0, 120); mechine.AddTarget(mechanismGound);
            mechanismGound.CurrentAnimation = new Animation("images/items/mechineWall1", new Point(90, 50));
            mechine.AdditionAction += CameraFocus;
            Monster m = new Monster(this);
            m.Position = new Vector2(300, 161);
            actors.Add(m);

            MusicManager.AddMusic("Music/doorlock", "doorlock", 0.001f, false);
            _bloom = _gameClass._BloomComponent;

            _song = MusicManager.LoadSong("Music/stage6");
            if (GameClass._musicString == "Music/stage6")
            { }
            else
            {
                GameClass._musicString = "Music/stage6";
                MediaPlayer.Play(_song);
            }
            MediaPlayer.IsRepeating = true;
           // MediaPlayer.Volume = 1f;

            actors.Add(smokePlume);

            _startProtal = new ProtalCommand("stage8", "Stage8Portal_1");
        }


        public override void Restart()
        {
            //base.Restart();
            StageManager.Save.StageStateTable.Remove(StageManager.CurrentStateID);
            StageManager.ProtalCommand = new ProtalCommand("stage8", "Stage8Portal_1");
        }

        private void UpdateSmokePlume(float dt)
        {
            timeTillPuff -= dt;
            if (timeTillPuff < 0)
            {
                Vector2 where = Vector2.Zero;
                // add more particles at the bottom of the screen, halfway across.
                //this.Game.GraphicsDevice.Viewport;
                where.X = GameClass.RandomBetween(_scenceBound.Left, _scenceBound.Right);
                where.Y = GameClass.RandomBetween(_scenceBound.Top, _scenceBound.Bottom);
                smokePlume.AddParticles(where);

                // and then reset the timer.
                timeTillPuff = TimeBetweenSmokePlumePuffs;
            }
        }



        void CameraFocus()
        {
            FocusTarget f = new FocusTarget(new Vector2(1783, -16));
            camera.Focus = f;
        }

        public override void OnDraw(SpriteBatch spriteBatch, int delta)
        {

            base.OnDraw(spriteBatch, delta);

            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, camera.Transform);
           // basePos = player.Position;

            foreach (VisibleObject b in actors)
                b.OnDraw(spriteBatch, camera.Transform);

            spriteBatch.End();

            //spriteBatch.Begin();
            //spriteBatch.DrawString(Font, "Po" + mechanismGound.PositiveActionCount + "Ne" + mechanismGound.NegetiveActionCount, new Vector2(50, 50), Color.Black);
            //spriteBatch.DrawString(Font, "temp" + mechanismGound.tempCount + "count"+mechanismGound.PositiveAction.Count, new Vector2(50, 50), Color.Black);
            //spriteBatch.End();

        }

        public override void Update(int delta)
        {
            base.Update(delta);

            if (camera.Focus != player && focusTime > 0)
                focusTime--;
            if (focusTime == 0)
                camera.Focus = player;

            UpdateSmokePlume(0.01f);
            //camera.Update(delta);
            //a.Update(delta);
            //  MusicManager.FadeInMusic("first");
            //MusicManager.FadeOutMusic("first");
        }

        public override Stage Clone()
        {
            Stage8 stage = new Stage8(Game);
            stage.InitializeScenes();
            return stage;
        }
    }
}
